// main class that manages all the menus.
function XF_MenuManager()
{
	// public variables
	this.ScriptsURL = "";
	
	// private variables
	this.Menus = new Array();
	this.ShownMenu = null;
	this.Enabled = true;
	this.TimerId = null;
	this.CanHide = false;
}

XF_MenuManager.prototype.IsMenuOpen = function ()
{
	if (this.ShownMenu != null)
	{
		if (this.ShownMenu.Popup != null)
		{
			if (this.ShownMenu.Popup.isOpen)
			{
				return true;
			}
			else
			{
				this.ShownMenu = null;
				return this.IsInlineMenuOpen();
			}
		}
		else
		{
			return this.IsInlineMenuOpen();
		}
	}
	else
	{
		return this.IsInlineMenuOpen();
	}
}

XF_MenuManager.prototype.IsInlineMenuOpen = function ()
{
	for (menuIdx in this.Menus)
	{
		menu = this.Menus[menuIdx];
		if (menu.IsInline)
		{
			if (menu.ShownMenu != null)
			{
				if (menu.Items[menu.ShownMenu].SubMenu.Popup != null)
				{
					if (menu.Items[menu.ShownMenu].SubMenu.Popup.isOpen)
					{
						return true;
					}
				}
				
				menu.ShownMenu = null;
			}
		}
	}
	
	return false;
}

// Hide All shown menu
XF_MenuManager.prototype.HideAll = function ()
{
	for (menuIdx in this.Menus)
	{
		menu = this.Menus[menuIdx];
		if (!menu.IsInline)
		{
			if (menu.Popup != null)
			{
				menu.Popup.hide();
			}
		}
		else
		{
			if (menu.ShownMenu != null)
			{
				if (menu.Items[menu.ShownMenu].SubMenu.Popup != null)
				{
					menu.Items[menu.ShownMenu].SubMenu.Popup.hide();
				}
				
				menu.ShownMenu = null;
			}
		}
	}
}

XF_MenuManager.prototype.InsertInlineMenu = function (menuName)
{
	var menu;
	var inlineMenu;
	var inlinePos;
	var menuIdx, inlineIdx;
	var html;
	
	for (menuIdx in this.Menus)
	{
		menu = this.Menus[menuIdx];
		if (menu.Name == menuName && !menu.IsInline)
		{
			if (xf_Is.nav4)
			{
				html =  "<IMG NAME=\"xf_anchor_" + menu.Name + "\" SRC=\"" + this.ScriptsURL + "/fill.gif\" width=\"" + menu.Width() + "\" height=\"" + menu.Height() + "\">&nbsp;</IMG>";
			}
			else
			{
				html =  "<SPAN ID=\"xf_anchor_" + menu.Name + "\">&nbsp;</SPAN>";

				document.write(menu.GetHTMLDiv());
			}
			
			menu.IsInline = true;
		}
	}
}

XF_MenuManager.prototype.ResizeMenu = function (menuName)
{
	var menu;
	var inlineMenu;
	var inlinePos;
	var menuIdx, inlineIdx;
	var html;
	
	for (menuIdx in this.Menus)
	{
		menu = this.Menus[menuIdx];
		if (menu.Name == menuName)
		{
			document.all['xf_div_' + menuIdx].style.height = document.all['xf_div_' + menuIdx].contentWindow.document.all['xf_table'].clientHeight;
		}
	}
}

XF_MenuManager.prototype.ResizeAllMenu = function ()
{
	var menu;
	var inlineMenu;
	var inlinePos;
	var menuIdx, inlineIdx;
	var html;
	
	for (menuIdx in this.Menus)
	{
		menu = this.Menus[menuIdx];
		if (document.all['xf_div_' + menuIdx] != null)
		{
			document.all['xf_div_' + menuIdx].style.height = document.all['xf_div_' + menuIdx].contentWindow.document.all['xf_table'].clientHeight;
		}
	}
}

XF_MenuManager.prototype.AddMenu = function (menu)
{
	menu.Index = this.Menus.length;
	this.Menus[this.Menus.length] = menu;
}

XF_MenuManager.prototype.ShowInlineMenus = function ()
{
	var menu;
	var menuIdx, inlineIdx;

	this.HideAll();
	
	for (menuIdx in this.Menus)
	{
		menu = this.Menus[menuIdx];
		if (menu.IsInline)
		{
			var anchor;

			anchor = XF_GetElementById(this.InlineMenus[inlineIdx].MenuId + 'anchor');

			if (anchor != null)
			{
				var xPos;
				var yPos;
				
				xPos = XF_GetXPosition(anchor);
				yPos = XF_GetYPosition(anchor);

				menu.Show(window, xPos, yPos);
			}
		}
	}
}

XF_MenuManager.prototype.ShowMenu = function (menuName, xPos, yPos)
{
	var menu;
	var menuIdx, inlineIdx;

	this.HideAll();
	
	for (menuIdx in this.Menus)
	{
		menu = this.Menus[menuIdx];
		
		if (menu.Name == menuName && !menu.IsInline)
		{
			this.ShownMenu = menu;
			
			menu.Show(window, xPos, yPos);
		}
	}
}

XF_MenuManager.prototype.HideMenu = function (menuIndex)
{
	var menu;
	
	if (typeof(menuIndex) == 'string')
	{
		menu = this.GetMenu(menuIndex);
		menuIndex = menu.Index;
	}
	else
	{
		menu = this.Menus[menuIndex];
	}
	
	return menu.Hide();
}


// menu item class
function XF_MenuItem(text, action)
{
	this.Text = text;
	if (typeof(action) != 'undefined')
	{
		this.Action = action;
	}
	else
	{
		this.Action = null;
	}

	this.Function = null;
	this.Arguments = null;

	this.SubMenu = null;
	this.Row = null;
}


// menu class
function XF_Menu(menuName)
{
	// public variables
	this.Name = menuName;
	this.StyleSheet = null;
	this.CSSPrefix = null;
	this.Window = null;
	this.HeightFix = 0;
	this.WidthFix = 0;
	this.PaddingTop = 0;
	this.PaddingLeft = 0;
	this.PaddingRight = 0;
	this.PaddingBottom = 0;
	this.ItemWidth = 100;
	this.ItemHeight = 100;
	this.ItemNoWrap = 'nowrap';
	this.SeparatorColor = 'silver';
	this.SubMenuImageURL = null;
	this.OverFlowWidth = null;
	this.SubMenuImageWidth = 5;
	this.CellPadding = 0;
	this.CellSpacing = 0;
	this.SubMenuSpacing = 0;
	this.SubMenuVSpacing = 0;
	this.BeforeMenuImageURL = null;
	this.AfterMenuImageURL = null;
	
	// private variables
	this.Index = 0;
	this.Items = new Array();
	this.Popup = null;
	this.HTML = null;
	this.XPos = null;
	this.YPos = null;
	this.ShownMenu = null;
	this.IsInline = false;
	this.CurrentItem = null;
	this.bHasSubMenu = null;
}

XF_Menu.prototype.HasSubMenu = function ()
{
	var itemIdx;
	
	if (this.bHasSubMenu == null)
	{
		this.bHasSubMenu = false;
		
		for (itemIdx in this.Items)
		{
			if (this.Items[itemIdx] != null)
			{
				if (this.Items[itemIdx].SubMenu != null)
				{
					this.bHasSubMenu = true;
					break;
				}
			}
		}
	}
	
	return this.bHasSubMenu;
}

XF_Menu.prototype.Width = function ()
{
	return this.PaddingLeft + this.ItemWidth + this.PaddingRight + this.WidthFix; 
}

XF_Menu.prototype.Height = function ()
{
	return this.PaddingTop + (this.ItemHeight * this.Items.length) + this.PaddingBottom + this.HeightFix;
}

XF_Menu.prototype.GetMenuClass = function ()
{
	if (this.CSSPrefix != null)
	{
		return "class=\"" + this.CSSPrefix + "Menu\"";
	}
	else
	{
		return "";
	}
}

XF_Menu.prototype.GetTableClass = function ()
{
	if (this.CSSPrefix != null)
	{
		return "class=\"" + this.CSSPrefix + "Table\"";
	}
	else
	{
		return "";
	}
}

XF_Menu.prototype.GetMenuItemClass = function ()
{
	if (this.CSSPrefix != null)
	{
		return "class=\"" + this.CSSPrefix + "Item\"";
	}
	else
	{
		return "";
	}
}

XF_Menu.prototype.GetMenuItemOverClass = function ()
{
	if (this.CSSPrefix != null)
	{
		return "class=\"" + this.CSSPrefix + "ItemOver\"";
	}
	else
	{
		return "";
	}
}

XF_Menu.prototype.GetMenuTextClass = function ()
{
	if (this.CSSPrefix != null)
	{
		return "class=\"" + this.CSSPrefix + "Text\"";
	}
	else
	{
		return "";
	}
}

XF_Menu.prototype.GetMenuTextOverClass = function ()
{
	if (this.CSSPrefix != null)
	{
		return "class=\"" + this.CSSPrefix + "TextOver\"";
	}
	else
	{
		return "";
	}
}

XF_Menu.prototype.GetSeparatorTableClass = function ()
{
	if (this.CSSPrefix != null)
	{
		return "class=\"" + this.CSSPrefix + "Separator\"";
	}
	else
	{
		return "";
	}
}

XF_Menu.prototype.Show = function (win, xPos, yPos)
{
	if (!win.xf_MainWindow.xf_MenuManager.Enabled)
	{
		return;
	}
	if (!this.IsInline)
	{
		if (this.Popup == null)
		{
			this.Popup = win.createPopup();
			
			this.HTML = this.GetHTML();
			this.Popup.document.write(this.HTML);
			//alert(this.HTML)
		}

		this.Popup.show(xPos, yPos, this.Width(), this.Height(), win.document.body);
		this.XPos = xPos;
		this.YPos = yPos;
		
		if (this.CurrentItem != null)
		{
			XF_Out(this.Popup, this.Index, this.CurrentItem, this.Items[this.CurrentItem].Row);
		}
	}
}

XF_Menu.prototype.GetHTMLDiv = function ()
{
	if (this.HTML == null)
	{
		if (this.ItemNoWrap == '')
		{
			this.HTML = "<IFRAME id=\"xf_div_" + this.Index + "\" frameborder=\"no\" width=\"" + this.Width() + "\" SCROLLING=\"no\" src=\"\" style=\"height:1px\">";
		}
		else
		{
			this.HTML = "<IFRAME id=\"xf_div_" + this.Index + "\" frameborder=\"no\" width=\"" + this.Width() + "\" height=\"" + this.Height() + "\" SCROLLING=\"no\" src=\"\">";
		}
		this.HTML += "</IFRAME>";
		this.HTML += "<script language=\"javascript\">";
		this.HTML += "document.all[\"xf_div_" + this.Index + "\"].contentWindow.document.write(\"" + this.GetHTML().replace(/\"/g, '\\"').replace(/script/g, 'scr" + "ipt') + "\");";
		this.HTML += "document.all[\"xf_div_" + this.Index + "\"].contentWindow.document.close();";
		this.HTML += "</script>";
	}

	return this.HTML;
}

XF_Menu.prototype.GetHTML = function ()
{
	var html;

	html = "<HTML><HEAD>";
	if(this.StyleSheet != null)
	{
		html += "<LINK rel=\"stylesheet\" type=\"text/css\" href=\"" + this.StyleSheet + "\"/>";
	}
	html += "<script language=\"javascript\">";
	html += "var xf_MainWindow;";
	html += "var XF_Over;";
	html += "var XF_Out;";
	html += "var XF_Click;";
	html += "xf_MainWindow = window.parent.xf_MainWindow;";
	html += "XF_Over = xf_MainWindow.XF_Over;";
	html += "XF_Out = xf_MainWindow.XF_Out;";
	html += "XF_Click = xf_MainWindow.XF_Click;";
	html += "</script>";
	html += "</HEAD><BODY " + this.GetMenuClass() + " SCROLL=\"no\">";
	html += this.GetHTMLBody();
	html += "<script language=\"javascript\">";
	html += "document.attachEvent('onmouseover', xf_MainWindow.XF_MenuEnable);";
	html += "document.attachEvent('onmouseout', xf_MainWindow.XF_MenuDisable);";
	html += "</script>";
	html += "</BODY></HTML>";
	
	return html;
}

XF_Menu.prototype.GetHTMLBody = function ()
{
	var html;
	var itemIdx;
	
	html = "<table id='xf_table' border=\"0\" " + this.GetTableClass() + " cellspacing=\"" + this.CellSpacing + "\" cellpadding=\"" + this.CellPadding + "\" >";
	for (itemIdx in this.Items)
	{
		if (this.Items[itemIdx] != null)
		{
			html += "<tr>";
			html += "<td " + this.ItemNoWrap + " width='100%' id=\"xf_row_" + itemIdx + "\" " + this.GetMenuItemClass() + " onclick=\"XF_Click(window, '" + this.Index + "', '" + itemIdx + "', this);\" onmouseover=\"XF_Over(window, '" + this.Index + "', '" + itemIdx + "', this);\" onmouseout=\"XF_Out(window, '" + this.Index + "', '" + itemIdx + "', this);\" " + this.ItemNoWrap + ">";
			if (this.SubMenuImageURL != null && this.HasSubMenu())
			{
				html += "<table border='0' cellpadding='0' cellspacing='0' width='100%'>";
				html += "<tr>";
				if (this.BeforeMenuImageURL != null)
				{
					html += "<td valign=\"top\"><img src='" + this.BeforeMenuImageURL + "'/></td>";
				}
				html += "<td " + this.ItemNoWrap + " width='100%' " + this.GetMenuTextClass() + " >";
				if (this.OverFlowWidth != null)
				{
					html += "<div " + this.ItemNoWrap + " style=\"width:" + this.OverFlowWidth + "px;overflow:hidden;\">" + this.Items[itemIdx].Text + "</div></td>";
				}
				else
				{
					html += this.Items[itemIdx].Text + "</td>";
				}
				if (this.Items[itemIdx].SubMenu != null)
				{
					//alert(this.SubMenuImageURL);
					html += "<td width='" + this.SubMenuImageWidth + "px' align='left'><img src='" + this.SubMenuImageURL + "' border='0' /></td>";
				}
				else
				{
					html += "<td width='" + this.SubMenuImageWidth + "px' align='left'><img src=\"" + xf_MainWindow.xf_MenuManager.ScriptsURL + "/fill.gif\" width='" + this.SubMenuImageWidth + "px' height='1'/></td>";
				}
				html += "</tr>";
				html += "</table>";
			}
			else
			{
				if (this.AfterMenuImageURL != null)
				{
					html += "<table border='0' cellpadding='0' cellspacing='0' width='100%'>";
					html += "<tr>";
					if (this.BeforeMenuImageURL != null)
					{
						html += "<td valign=\"top\"><img src='" + this.BeforeMenuImageURL + "'/></td>";
					}
					html += "<td " + this.ItemNoWrap + " width='100%' " + this.GetMenuTextClass() + " >";
					if (this.OverFlowWidth != null)
					{
						html += "<div " + this.ItemNoWrap + " style=\"width:" + this.OverFlowWidth + "px;overflow:hidden;\">" + this.Items[itemIdx].Text + "</div>";
					}
					else
					{
						html += this.Items[itemIdx].Text
					}
					html += "</td><td width='" + this.SubMenuImageWidth + "px' align='left'><img src='" + this.AfterMenuImageURL + "' border='0' /></td></tr></table>";
				}
				else
				{
					if (this.BeforeMenuImageURL != null)
					{
						html += "<table border='0' cellpadding='0' cellspacing='0' width='100%'>";
						html += "<tr>";
						html += "<td valign=\"top\"><img src='" + this.BeforeMenuImageURL + "'/></td>";
						html += "<td " + this.ItemNoWrap + " width='100%' " + this.GetMenuTextClass() + " >";
					}
					if (this.OverFlowWidth != null)
					{
						html += "<div " + this.ItemNoWrap + " style=\"width:" + this.OverFlowWidth + "px;overflow:hidden;\">" + this.Items[itemIdx].Text + "</div>";
					}
					else
					{
						html += this.Items[itemIdx].Text
					}
					if (this.BeforeMenuImageURL != null)
					{
						html += "</td></tr></table>";
					}
				}
			}
			html += "</td>";
			html += "</tr>";
		}
		else
		{
			html += "<tr>";
			html += "<td id=\"xf_row_" + itemIdx + "\" " + this.GetMenuItemClass() + " >";

			html += "<table border=\"0\" bgcolor='" + this.SeparatorColor + "' cellpadding=\"0\" cellspacing=\"0\" " + this.GetSeparatorTableClass() + ">";
			html += "<tr style=\"height:1px;\">";
			html += "<td width=\"100%\"><img src=\"" + xf_MainWindow.xf_MenuManager.ScriptsURL + "/fill.gif\" width='100%' height='1'/></td>";
			html += "</tr></table>";

			html += "</td>";
			html += "</tr>";
		}
	}

	html += "</table>";

	return html;
}

XF_Menu.prototype.Hide = function ()
{
	if (this.Popup != null)
	{
		this.Popup.hide();
	}
}

XF_Menu.prototype.XPosition = function ()
{
return 0;
	if (this.IsInline)
	{
		if (this.XPos == null || this.YPos == null)
		{
			var menuLayer = XF_GetElementById("xf_div_" + this.Index);

			this.XPos = parseInt(XF_GetXPosition(menuLayer).toString());
			this.YPos = parseInt(XF_GetYPosition(menuLayer).toString());
		}
		
		return this.XPos;
	}
	else
	{
		return 0;
	}
}

XF_Menu.prototype.YPosition = function ()
{
return 0;
	if (this.IsInline)
	{
		if (this.XPos == null || this.YPos == null)
		{
			var menuLayer = XF_GetElementById("xf_div_" + this.Index);

			this.XPos = parseInt(XF_GetXPosition(menuLayer).toString());
			this.YPos = parseInt(XF_GetYPosition(menuLayer).toString());
		}
		
		return this.YPos;
	}
	else
	{
		return 0;
	}
}

XF_Menu.prototype.Move = function (xPos, yPos)
{
}

XF_Menu.prototype.AddSeparator = function ()
{
	this.Items[this.Items.length] = null;
}

XF_Menu.prototype.AddItem = function(menuItem)
{
	this.Items[this.Items.length] = menuItem;
}

function XF_Click(win, menuIndex, itemIdx, row)
{
	var menu;
	var menuItem;
	var menuRow;
	var xPos;
	var yPos;
	
	menu = xf_MenuManager.Menus[menuIndex];
	
	menuItem = menu.Items[itemIdx];
	if (menuItem == null)
	{
		return;
	}
	else
	{
		if (menuItem.Action != null)
		{
			xf_MenuManager.HideAll();
			window.location.href = menuItem.Action;
		}
		else
		{
			if (menuItem.Function != null)
			{
				xf_MenuManager.HideAll();

				fct = menuItem.Function + "(";
				if (menuItem.Arguments != null)
				{
					fct += menuItem.Arguments;
				}
				fct += ")";

				eval(fct);
			}
		}
	}
}

function XF_Over(win, menuIndex, itemIdx, row)
{
	var menu;
	var menuItem;

	xf_MainWindow.XF_MenuEnable();
	//xf_MainWindow.setTimeout("xf_MainWindow.XF_MenuEnable()", 450);
	menu = xf_MenuManager.Menus[menuIndex];
	menuItem = menu.Items[itemIdx];
	menuItem.Row = row;

	menu.CurrentItem = itemIdx;

	if (menu.ShownMenu != null)
	{
		menu.Items[menu.ShownMenu].SubMenu.Hide();
	}
	
	if (menu.CSSPrefix != null)
	{
		row.className = menu.CSSPrefix + "ItemOver";
	}
	
	if (menuItem.SubMenu != null)
	{
		menu.ShownMenu = itemIdx;
		//menuItem.SubMenu.Show(win, menu.XPosition() + menu.Width() + menu.SubMenuSpacing, menu.YPosition() + (itemIdx * menu.ItemHeight) + menu.SubMenuVSpacing);
		menuItem.SubMenu.Show(win, menu.XPosition() + menu.Width() + menu.SubMenuSpacing, XF_GetYPosition(row));
	}
}

function XF_Out(win, menuIndex, itemIdx, row)
{
	xf_MainWindow.XF_MenuDisable();
	menu = xf_MenuManager.Menus[menuIndex];
	
	if (menu.CSSPrefix != null)
	{
		if (row != null)
		{
			row.className = menu.CSSPrefix + "Item";
		}
	}

	menu.CurrentItem = null;
}

function XF_MenuHideAll()
{
	xf_MenuManager.HideAll();
}

function XF_MenuDisable()
{
	xf_MainWindow.xf_MenuManager.CanHide = true;
	if (xf_MainWindow.xf_MenuManager.TimerId != null)
	{
		xf_MainWindow.clearTimeout(xf_MainWindow.xf_MenuManager.TimerId);
		xf_MainWindow.xf_MenuManager.TimerId = null;
	}
	xf_MainWindow.xf_MenuManager.TimerId = xf_MainWindow.setTimeout("XF_MenuRealDisable()", 750);
}

function XF_MenuRealDisable()
{
	if (xf_MainWindow.xf_MenuManager.CanHide)
	{
		xf_MainWindow.xf_MenuManager.TimerId = null;
		xf_MenuManager.HideAll();
//		xf_MainWindow.xf_MenuManager.Enabled = false;
	}
}

function XF_MenuEnable()
{
	xf_MainWindow.xf_MenuManager.CanHide = false;
	if (xf_MainWindow.xf_MenuManager.TimerId != null)
	{
		xf_MainWindow.clearTimeout(xf_MainWindow.xf_MenuManager.TimerId);
		xf_MainWindow.xf_MenuManager.TimerId = null;
	}
	xf_MainWindow.xf_MenuManager.Enabled = true;
//	xf_MainWindow.status = 'Enable ' + xf_MainWindow.xf_MenuManager.Enabled;
}

var xf_MenuManager = new XF_MenuManager();
var xf_MainWindow = window;

//xf_DEBUG.Push();

document.attachEvent('onmouseout', XF_MenuDisable);
//document.attachEvent('onmouseover', XF_MenuEnable);
window.attachEvent('onfocus', XF_MenuEnable);
window.attachEvent('onactive', XF_MenuEnable);
window.attachEvent('onclick', XF_MenuEnable);
